package com.example.easybuy.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.easybuy.pojo.EasybuyProduct;
import com.example.easybuy.pojo.EasybuyUserAddress;
import com.example.easybuy.service.BuyCarService;
import com.example.easybuy.service.EasybuyProductService;
import com.example.easybuy.service.EasybuyUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buyCar")
public class BuyCarController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    BuyCarService buyCarService;

    @Autowired
    EasybuyProductService easybuyProductService;

    @Autowired
    EasybuyUserAddressService easybuyUserAddressService;

    @RequestMapping("/getCarProductInPerson")
    public Map<String, Object> getCarProductInPerson(@RequestParam("userId") Integer userId) {
        return buyCarService.getBuyCar(userId);
    }

    @RequestMapping("/addProductToBuyCar")
    public boolean addProductToBuyCar(@RequestParam("userId") Integer userId,
                                      @RequestParam("productId") Integer productId,
                                      @RequestParam("addNum") Integer addNum) {
        return buyCarService.addProductToBuyCar(userId, productId, addNum);
    }

    /**
     * 弃用  选择购买改为购买购物车全部商品
     * 在Redis中添加要购买的   key 用户 buyCar + userId  value Map :  商品productList 数量stockMap  用户地址userAddressList
     *
     * @param userId
     * @param products
     * @return
     */
    @RequestMapping("/selectedProduct")
    public String selectedProduct(@RequestParam("userId") Integer userId,
                                  @RequestParam("products") String products) {
        Map<String, Integer> map = (Map<String, Integer>) JSONObject.parse(products);
        Map<String, Object> redisMap = new HashMap<>();
        Map<String, Integer> stockMap = new HashMap<>();
        List<EasybuyProduct> productList = new ArrayList<>();

        for (String productId : map.keySet()) {
            Integer integer = Integer.parseInt(productId);
            EasybuyProduct product = easybuyProductService.getProductById(integer);
            productList.add(product);
            Integer integer1 = map.get(productId);
            stockMap.put(productId, integer1);
        }
        List<EasybuyUserAddress> userAddressList = easybuyUserAddressService.getAddressByUserId(userId);
        redisMap.put("stockMap", stockMap);
        redisMap.put("productList", productList);
        redisMap.put("userAddressList", userAddressList);
        redisTemplate.opsForValue().set("buyCar" + userId, redisMap);
        return "true";
    }

    /**
     * 删除购物车中的商品
     *
     * @param productId 要删除的商品Id
     * @param userId    用户Id
     * @return
     */
    @RequestMapping("/delCarProduct")
    public boolean delCarProduct(@RequestParam("productId") Integer productId, @RequestParam("userId") Integer userId) {
        return buyCarService.delProduct(userId, productId);
    }

    /**
     * 用户提交订单视图数据
     *
     * @param userId
     * @return
     */
    @RequestMapping("/createViewForOrderMap")
    public Map<String, Object> createViewForOrderMap(@RequestParam("userId") Integer userId) {
        return buyCarService.createViewForOrderMap(userId);
    }

    //添加收藏夹的数据
    @RequestMapping("/addFavoritesList")
    public Object addFavoritesList(@RequestParam(value = "uid") Integer userId, @RequestParam(value = "pid") Integer addId) {
        /*Integer userId=24;
        Integer addId=769;*/
        boolean boo = false;
        //不存在默认为true,添加；存在则删除；
        List<EasybuyProduct> productList = (List<EasybuyProduct>) redisTemplate.opsForValue().get("Fav:" + userId);
        if (productList == null) {
            productList = new ArrayList<>();
        }

        for (int i = 0; i < productList.size(); i++) {
            Integer pid = productList.get(i).getId();
            if (pid.equals(addId)) {
                boo = true;
                break;
            }
        }

        if (!boo) {
            productList.add(easybuyProductService.getProductById(addId));
        }
        String key = "Fav:" + userId;
        redisTemplate.opsForValue().set(key, productList);
        return boo;
    }

    //获取收藏夹的数据
    @RequestMapping("/getFavoritesList")
    public Object getFavoritesList(Integer userId) {
        List<EasybuyProduct> productList = (List<EasybuyProduct>) redisTemplate.opsForValue().get("Fav:" + userId);
        return productList;
    }

    //删除收藏夹的数据
    //List每remove掉一个元素以后，后面的元素都会向前移动 ，此时如果执行i++，则刚刚移过来的元素没有被读取。
    @RequestMapping("/delFavoritesList")
    public Object delFavoritesList(String userId, String delId) {
       /* Integer userId=24;
        Integer delId=769;*/
        //不存在默认为true,添加；存在则删除；
        List<EasybuyProduct> productList = (List<EasybuyProduct>) redisTemplate.opsForValue().get("Fav:" + userId);
        for (int i = 0; i < productList.size(); i++) {
            Integer pid = productList.get(i).getId();
            if (pid==Integer.parseInt(delId)) {
                productList.remove(i);
                break;
            }
        }
        String key = "Fav:" + userId;
        redisTemplate.opsForValue().set(key, productList);
        int num = productList.size();
        return num;
    }
}
